<?php
/**
 * 搜索
 */
namespace App\Validation;

use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Numericality;
use Phalcon\Validation\Validator\InclusionIn;

class SearchValidation extends BaseValidation
{
	public function initialize()
	{
		$this->add(
			"groupId",
			new PresenceOf(
				  [
					  "message" => ":field ",
                      "allowEmpty" => true,
				  ]
			)
		);
        
		$this->add(
			"areaId",
			new PresenceOf(
				  [
					  "message" => ":field ",
                      "allowEmpty" => true,
				  ]
			)
		);        
        
		$this->add(
			"words",
			new PresenceOf(
				  [
					  "message" => ":field ",
                      "allowEmpty" => true,
				  ]
			)
		);
        
		$this->add(
			"p",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);
		
		$this->add(
			"page_num",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);
        
        $this->add(
			"tx",
			new PresenceOf(
				  [
					  "message" => ":field ",
                      "allowEmpty" => true,
				  ]
			)
		);
	}

	//验证之前
	public function beforeValidation($data, $entity, $messages)
    {
    	parent::beforeValidation($data, $entity, $messages);
    	
        $data->words = isset($data->words) ? $data->words : "";
        $data->groupId = isset($data->groupId) ? $data->groupId : 0;
        $data->areaId = isset($data->areaId) ? $data->areaId : 0;
        $data->tx = isset($data->tx) ? $data->tx : '';
        return true;
    }
}